
#include "fresnel_cos_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"  //


namespace optiling {

const uint32_t BLOCK_SIZE = 32; // 最小计算单元, 32字节

static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    FresnelCosTilingData tiling;
    int32_t NUM = 4;  // 需要用到的buffer数量（把ub划分为多少buffer） 一个数据算+1，如果使用doublebuffer一个数据+2，tempbuffer+1
    uint32_t sizeofdatatype;  // 一个元素占的byte数
    uint32_t totalLengthAligned;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto socVersion = ascendcPlatform.GetSocVersion();
    uint64_t ub_size;   // 253952 字节
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);  // 获取ub size   253952 字节
    auto aivNum = ascendcPlatform.GetCoreNum();  // 1

    auto totalLength = context->GetInputShape(0)->GetStorageShape().GetShapeSize();  // 输入元素个数
    auto dt = context->GetInputDesc(0)->GetDataType();  //获取矢量计算单元 或 ai core数量（核数）

    if(dt == ge::DT_INT8){  // 不同类型的字节数
        sizeofdatatype = 1;
    }else if(dt == ge::DT_FLOAT16 || dt == ge::DT_BF16){

        sizeofdatatype = 2;


        NUM = 27;
    }else{
        sizeofdatatype = 4;
        NUM = 14;
    }


    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;  // 一个32B BLOCK包含输入元素的个数
    std::cout << "ALIGN_NUM: " << ALIGN_NUM << std::endl;
    
    uint32_t tiling_size = ((ub_size) / BLOCK_SIZE / 2) / NUM;  // 尽量占满UB条件下，每个ub切块上的有多少个32B block
    tiling_size = tiling_size <= 8 ? tiling_size : tiling_size / 8 * 8;  // 256B对齐（矢量计算单元每次取256B）
    std::cout << "tiling_size: " << tiling_size << std::endl;

    uint32_t block_size = tiling_size * ALIGN_NUM;  // 一个ub切块上的元素个数
    std::cout << "block_size: " << block_size << std::endl;


    uint32_t core_size = (totalLength / aivNum) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);  // 核间分并且256B对齐
    std::cout << "core_size: " << core_size << std::endl;

    uint32_t core_remain = totalLength - aivNum * core_size;  // 尾部数据元素个数
    std::cout << "core_remain: " << core_remain << std::endl;

    tiling.set_ALIGN_NUM(ALIGN_NUM);
    tiling.set_tiling_size(tiling_size);
    tiling.set_block_size(block_size);
    tiling.set_core_size(core_size);
    tiling.set_core_remain(core_remain);
    context->SetBlockDim(1);

    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    return ge::GRAPH_SUCCESS;
}
}



namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class FresnelCos : public OpDef {
public:
    explicit FresnelCos(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(FresnelCos);
}
